package com.xilihui.xlh.business.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.activitys.store.StoreGoodDetailsActivity;
import com.xilihui.xlh.business.entities.StoreClassEntity;
import com.xilihui.xlh.business.entities.StoreHomeDataEntity;
import com.xilihui.xlh.business.requests.StoreRequest;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.component.recyclerview.BaseViewHolder;
import com.xilihui.xlh.component.recyclerview.LinearLayoutColorDivider;
import com.xilihui.xlh.constant.UrlConst;
import com.xilihui.xlh.core.app.BaseCompatFragment;
import com.xilihui.xlh.core.glide.ImageHelper;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/8/31.
 * 商城全部
 */

public class StoreAllFragment extends BaseCompatFragment implements OnRefreshListener, OnLoadMoreListener {

    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;
    
    Banner banner;
    RecyclerView headRecycler;
    BaseAdapter<StoreHomeDataEntity.HotGoodsBean> headAdapter;
    BaseAdapter<StoreHomeDataEntity.NewGoodsBean> beanBaseAdapter;
    
    ArrayList<StoreHomeDataEntity.HotGoodsBean> hotGoodsBeans = new ArrayList<>();
    ArrayList<StoreHomeDataEntity.NewGoodsBean> newGoodsBeans = new ArrayList<>();
    
    ArrayList<StoreHomeDataEntity.BannerBean> bannerBeans = new ArrayList<>();
    
    int page = 1;
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_store_all,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        
        smartRefreshLayout.setOnRefreshListener(this);
        smartRefreshLayout.setOnLoadMoreListener(this);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setLoadingMoreEnabled(false);
        initHeadView();
        getData(true);
        
        beanBaseAdapter = new BaseAdapter<StoreHomeDataEntity.NewGoodsBean>(getActivity(),newGoodsBeans) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycyler_item_shop_good_new;
            }

            @Override
            protected void getView(BaseViewHolder holder, final StoreHomeDataEntity.NewGoodsBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv);
                simpleDraweeView.setImageURI(UrlConst.baseUrl() + entity.getOriginal_img());
                
                holder.setText(R.id.tv_title,entity.getGoods_name());
                holder.setText(R.id.tv_num,"已售"+entity.getSales_sum()+"件");
                holder.setText(R.id.tv_price,"￥"+entity.getShop_price());
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), StoreGoodDetailsActivity.class);
                        intent.putExtra("goods_id",entity.getGoods_id());
                        startActivity(intent);
                    }
                });
                
            }
        };
        
        recyclerView.setAdapter(beanBaseAdapter);
        beanBaseAdapter.setAnimationsLocked(true);
        
    }
    
    public void initHeadView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.recycler_item_shop_all_head,null);
        
        banner = (Banner)view.findViewById(R.id.banner); 
        headRecycler = (RecyclerView)view.findViewById(R.id.recyclerview);
        
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        headRecycler.setLayoutManager(gridLayoutManager);
        headRecycler.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.tv_color_fff,R.dimen.divider_space,
                LinearLayout.HORIZONTAL));
        headRecycler.addItemDecoration(new LinearLayoutColorDivider(getResources(),R.color.tv_color_fff,R.dimen.divider_space,
                LinearLayout.VERTICAL));
        
        headAdapter = new BaseAdapter<StoreHomeDataEntity.HotGoodsBean>(getActivity(),hotGoodsBeans) {
            @Override
            protected int setLayoutId() {
                return R.layout.recycler_item_shop_good;
            }

            @Override
            protected void getView(BaseViewHolder holder, final StoreHomeDataEntity.HotGoodsBean entity, int position) {
                SimpleDraweeView simpleDraweeView = (SimpleDraweeView)holder.getView(R.id.sdv);
                simpleDraweeView.setImageURI(UrlConst.baseUrl()+entity.getOriginal_img());
                
                holder.setText(R.id.tv_title,entity.getGoods_name());
                holder.setText(R.id.tv_price,"￥"+entity.getShop_price());
                holder.setText(R.id.tv_num,"出售"+entity.getSales_sum()+"件");
                
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getActivity(), StoreGoodDetailsActivity.class);
                        intent.putExtra("goods_id",entity.getGoods_id());
                        startActivity(intent);
                    }
                });
                
                
                
            }
        };
        
       
        headRecycler.setAdapter(headAdapter);
        headAdapter.setAnimationsLocked(true);
        recyclerView.addHeaderView(view);
        
    }
    
    public void getData(boolean isLoading){

        StoreRequest.home(page)
                .compose(DoTransform.<StoreHomeDataEntity>applyScheduler(getActivity(),isLoading))
                .compose(this.<StoreHomeDataEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<StoreHomeDataEntity>(getActivity()) {
                    @Override
                    protected void onSuccess(StoreHomeDataEntity entity) {
                        
                        banner.setImages(entity.getBanner());
                        banner.setImageLoader(new ImageLoader() {
                            @Override
                            public void displayImage(Context context, Object path, ImageView imageView) {
                                StoreHomeDataEntity.BannerBean bannerBean = (StoreHomeDataEntity.BannerBean) path;
                                ImageHelper.display(context,imageView, UrlConst.baseUrl()+bannerBean.getAd_code());
                            }
                        });
                        banner.start();
                        
                        if(page==1){
                            hotGoodsBeans.clear();
                            hotGoodsBeans.addAll(entity.getHot_goods());
                            headAdapter.setList(hotGoodsBeans);
                            newGoodsBeans.clear();
                        }
                        
                        newGoodsBeans.addAll(entity.getNew_goods());
                        beanBaseAdapter.setList(newGoodsBeans);
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;   
        getData(false);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }
}
